home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / simplejson / decoder.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2008-10-13  |  8.5 KB  |  355 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. import re
  5. import sys
  6. from simplejson.scanner import make_scanner, pattern
  7.  
  8. try:
  9.     from simplejson._speedups import scanstring as c_scanstring
  10. except ImportError:
  11.     c_scanstring = None
  12.  
  13. FLAGS = re.VERBOSE | re.MULTILINE | re.DOTALL
  14.  
  15. def _floatconstants():
  16.     import struct
  17.     import sys
  18.     _BYTES = '7FF80000000000007FF0000000000000'.decode('hex')
  19.     if sys.byteorder != 'big':
  20.         _BYTES = _BYTES[:8][::-1] + _BYTES[8:][::-1]
  21.     
  22.     (nan, inf) = struct.unpack('dd', _BYTES)
  23.     return (nan, inf, -inf)
  24.  
  25. (NaN, PosInf, NegInf) = _floatconstants()
  26.  
  27. def linecol(doc, pos):
  28.     lineno = doc.count('\n', 0, pos) + 1
  29.     if lineno == 1:
  30.         colno = pos
  31.     else:
  32.         colno = pos - doc.rindex('\n', 0, pos)
  33.     return (lineno, colno)
  34.  
  35.  
  36. def errmsg(msg, doc, pos, end = None):
  37.     (lineno, colno) = linecol(doc, pos)
  38.     if end is None:
  39.         return '%s: line %d column %d (char %d)' % (msg, lineno, colno, pos)
  40.     
  41.     (endlineno, endcolno) = linecol(doc, end)
  42.     return '%s: line %d column %d - line %d column %d (char %d - %d)' % (msg, lineno, colno, endlineno, endcolno, pos, end)
  43.  
  44. _CONSTANTS = {
  45.     '-Infinity': NegInf,
  46.     'Infinity': PosInf,
  47.     'NaN': NaN,
  48.     'true': True,
  49.     'false': False,
  50.     'null': None }
  51.  
  52. def JSONConstant(match, context, c = _CONSTANTS):
  53.     s = match.group(0)
  54.     fn = getattr(context, 'parse_constant', None)
  55.     if fn is None:
  56.         rval = c[s]
  57.     else:
  58.         rval = fn(s)
  59.     return (rval, match.end())
  60.  
  61. pattern('(-?Infinity|NaN|true|false|null)')(JSONConstant)
  62.  
  63. def JSONNumber(match, context):
  64.     (integer, frac, exp) = match.groups()[:3]
  65.     if frac or exp:
  66.         if not getattr(context, 'parse_float', None):
  67.             pass
  68.         fn = float
  69.         if not frac:
  70.             pass
  71.         if not exp:
  72.             pass
  73.         res = fn(integer + '' + '')
  74.     elif not getattr(context, 'parse_int', None):
  75.         pass
  76.     fn = int
  77.     res = fn(integer)
  78.     return (res, match.end())
  79.  
  80. pattern('(-?(?:0|[1-9]\\d*))(\\.\\d+)?([eE][-+]?\\d+)?')(JSONNumber)
  81. STRINGCHUNK = re.compile('(.*?)(["\\\\\\x00-\\x1f])', FLAGS)
  82. BACKSLASH = {
  83.     '"': u'"',
  84.     '\\': u'\\',
  85.     '/': u'/',
  86.     'b': u'\x08',
  87.     'f': u'\x0c',
  88.     'n': u'\n',
  89.     'r': u'\r',
  90.     't': u'\t' }
  91. DEFAULT_ENCODING = 'utf-8'
  92.  
  93. def py_scanstring(s, end, encoding = None, strict = True, _b = BACKSLASH, _m = STRINGCHUNK.match):
  94.     if encoding is None:
  95.         encoding = DEFAULT_ENCODING
  96.     
  97.     chunks = []
  98.     _append = chunks.append
  99.     begin = end - 1
  100.     while None:
  101.         chunk = _m(s, end)
  102.         if chunk is None:
  103.             raise ValueError(errmsg('Unterminated string starting at', s, begin))
  104.         
  105.         end = chunk.end()
  106.         (content, terminator) = chunk.groups()
  107.         if content:
  108.             if not isinstance(content, unicode):
  109.                 content = unicode(content, encoding)
  110.             
  111.             _append(content)
  112.         
  113.         if terminator == '"':
  114.             break
  115.         elif terminator != '\\':
  116.             if strict:
  117.                 raise ValueError(errmsg('Invalid control character %r at', s, end))
  118.             else:
  119.                 _append(terminator)
  120.         
  121.         
  122.         try:
  123.             esc = s[end]
  124.         except IndexError:
  125.             raise ValueError(errmsg('Unterminated string starting at', s, begin))
  126.  
  127.         if esc != 'u':
  128.             
  129.             try:
  130.                 m = _b[esc]
  131.             except KeyError:
  132.                 raise ValueError(errmsg('Invalid \\escape: %r' % (esc,), s, end))
  133.  
  134.             end += 1
  135.         else:
  136.             esc = s[end + 1:end + 5]
  137.             next_end = end + 5
  138.             msg = 'Invalid \\uXXXX escape'
  139.             
  140.             try:
  141.                 if len(esc) != 4:
  142.                     raise ValueError
  143.                 
  144.                 uni = int(esc, 16)
  145.                 if uni <= uni:
  146.                     pass
  147.                 elif uni <= 56319 and sys.maxunicode > 65535:
  148.                     msg = 'Invalid \\uXXXX\\uXXXX surrogate pair'
  149.                     if not s[end + 5:end + 7] == '\\u':
  150.                         raise ValueError
  151.                     
  152.                     esc2 = s[end + 7:end + 11]
  153.                     if len(esc2) != 4:
  154.                         raise ValueError
  155.                     
  156.                     uni2 = int(esc2, 16)
  157.                     uni = 65536 + (uni - 55296 << 10 | uni2 - 56320)
  158.                     next_end += 6
  159.                 
  160.                 m = unichr(uni)
  161.             except ValueError:
  162.                 raise ValueError(errmsg(msg, s, end))
  163.  
  164.             end = next_end
  165.         continue
  166.         return (u''.join(chunks), end)
  167.  
  168. if not c_scanstring:
  169.     pass
  170. scanstring = py_scanstring
  171.  
  172. def JSONString(.0, context):
  173.     (string, end) = .0
  174.     encoding = getattr(context, 'encoding', None)
  175.     strict = getattr(context, 'strict', True)
  176.     return scanstring(string, end, encoding, strict)
  177.  
  178. pattern('"')(JSONString)
  179. WHITESPACE = re.compile('[ \\t\\n\\r]*', FLAGS)
  180. WHITESPACE_STR = ' \t\n\r'
  181.  
  182. def JSONObject(.0, context, _w = WHITESPACE.match, _ws = WHITESPACE_STR):
  183.     (s, end) = .0
  184.     pairs = { }
  185.     nextchar = s[end:end + 1]
  186.     if nextchar != '"':
  187.         if nextchar in _ws:
  188.             end = _w(s, end).end()
  189.             nextchar = s[end:end + 1]
  190.         
  191.         if nextchar == '}':
  192.             return (pairs, end + 1)
  193.         elif nextchar != '"':
  194.             raise ValueError(errmsg('Expecting property name', s, end))
  195.         
  196.     
  197.     end += 1
  198.     encoding = getattr(context, 'encoding', None)
  199.     strict = getattr(context, 'strict', True)
  200.     scan_once = JSONScanner
  201.     while True:
  202.         (key, end) = scanstring(s, end, encoding, strict)
  203.         if s[end:end + 1] != ':':
  204.             end = _w(s, end).end()
  205.             if s[end:end + 1] != ':':
  206.                 raise ValueError(errmsg('Expecting : delimiter', s, end))
  207.             
  208.         
  209.         end += 1
  210.         
  211.         try:
  212.             if s[end] in _ws:
  213.                 end += 1
  214.                 if s[end] in _ws:
  215.                     end = _w(s, end).end()
  216.                 
  217.         except IndexError:
  218.             pass
  219.  
  220.         
  221.         try:
  222.             (value, end) = scan_once(s, end, context)
  223.         except StopIteration:
  224.             raise ValueError(errmsg('Expecting object', s, end))
  225.  
  226.         pairs[key] = value
  227.         nextchar = s[end:end + 1]
  228.         if nextchar in _ws:
  229.             end = _w(s, end).end()
  230.             nextchar = s[end:end + 1]
  231.         
  232.         end += 1
  233.         if nextchar == '}':
  234.             break
  235.         elif nextchar != ',':
  236.             raise ValueError(errmsg('Expecting , delimiter', s, end - 1))
  237.         
  238.         
  239.         try:
  240.             if s[end] in _ws:
  241.                 end += 1
  242.                 if s[end] in _ws:
  243.                     end = _w(s, end).end()
  244.                 
  245.         except IndexError:
  246.             pass
  247.  
  248.         nextchar = s[end:end + 1]
  249.         end += 1
  250.         if nextchar != '"':
  251.             raise ValueError(errmsg('Expecting property name', s, end - 1))
  252.             continue
  253.     object_hook = getattr(context, 'object_hook', None)
  254.     if object_hook is not None:
  255.         pairs = object_hook(pairs)
  256.     
  257.     return (pairs, end)
  258.  
  259. pattern('{')(JSONObject)
  260.  
  261. def JSONArray(.0, context, _w = WHITESPACE.match, _ws = WHITESPACE_STR):
  262.     (s, end) = .0
  263.     values = []
  264.     nextchar = s[end:end + 1]
  265.     if nextchar in _ws:
  266.         end = _w(s, end).end()
  267.         nextchar = s[end:end + 1]
  268.     
  269.     if nextchar == ']':
  270.         return (values, end + 1)
  271.     
  272.     scan_once = JSONScanner
  273.     while True:
  274.         
  275.         try:
  276.             (value, end) = scan_once(s, end, context)
  277.         except StopIteration:
  278.             raise ValueError(errmsg('Expecting object', s, end))
  279.  
  280.         values.append(value)
  281.         nextchar = s[end:end + 1]
  282.         if nextchar in _ws:
  283.             end = _w(s, end).end()
  284.             nextchar = s[end:end + 1]
  285.         
  286.         end += 1
  287.         if nextchar == ']':
  288.             break
  289.         
  290.         if nextchar != ',':
  291.             raise ValueError(errmsg('Expecting , delimiter', s, end))
  292.         
  293.         
  294.         try:
  295.             if s[end] in _ws:
  296.                 end += 1
  297.                 if s[end] in _ws:
  298.                     end = _w(s, end).end()
  299.                 
  300.         continue
  301.         except IndexError:
  302.             continue
  303.         
  304.  
  305.         None<EXCEPTION MATCH>IndexError
  306.     return (values, end)
  307.  
  308. pattern('\\[')(JSONArray)
  309. ANYTHING = [
  310.     JSONObject,
  311.     JSONArray,
  312.     JSONString,
  313.     JSONConstant,
  314.     JSONNumber]
  315. JSONScanner = make_scanner(ANYTHING)
  316.  
  317. class JSONDecoder(object):
  318.     __all__ = [
  319.         '__init__',
  320.         'decode',
  321.         'raw_decode']
  322.     
  323.     def __init__(self, encoding = None, object_hook = None, parse_float = None, parse_int = None, parse_constant = None, strict = True):
  324.         self.encoding = encoding
  325.         self.object_hook = object_hook
  326.         self.parse_float = parse_float
  327.         self.parse_int = parse_int
  328.         self.parse_constant = parse_constant
  329.         self.strict = strict
  330.  
  331.     
  332.     def decode(self, s, _w = WHITESPACE.match):
  333.         (obj, end) = self.raw_decode(s, idx = _w(s, 0).end())
  334.         end = _w(s, end).end()
  335.         if end != len(s):
  336.             raise ValueError(errmsg('Extra data', s, end, len(s)))
  337.         
  338.         return obj
  339.  
  340.     
  341.     def raw_decode(self, s, **kw):
  342.         idx = kw.get('idx', 0)
  343.         context = kw.get('context', self)
  344.         
  345.         try:
  346.             (obj, end) = JSONScanner(s, idx, context)
  347.         except StopIteration:
  348.             raise ValueError('No JSON object could be decoded')
  349.  
  350.         return (obj, end)
  351.  
  352.  
  353. __all__ = [
  354.     'JSONDecoder']
  355.